package com.usian.controller;

import com.usian.service.StudentService;
import com.usian.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/us")
public class StudentController {

    @Autowired
    private StudentService userService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/import")
    @ResponseBody
    public String excelImport(@RequestParam(value="filename") MultipartFile file){
        try {
            List<String[]> stundets = POIUtils.readExcel(file);
            userService.addUser(stundets);
            return "导入成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "导入失败";
        }
    }
}
